﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebApiAttributeDemo.DAL.Entities;
using WebApiAttributeDemo.DAL.Repository;

namespace WebApiAttributeDemo.Controllers
{
    [RoutePrefix("api/blog")]
    [RoutePrefix("api/blogs")]
    public class BlogController : ApiController
    {
        private readonly IBlogRepository _blogRepository;

        public BlogController()
        {
            _blogRepository = new BlogRepository();
        }

        public BlogController(IBlogRepository blogRepository = null)
        {
            _blogRepository = blogRepository ?? new BlogRepository();
        }

        // GET api/<controller>
        public IEnumerable<BlogPost> Get()
        {
            return _blogRepository.GetAll();
        }

        // GET api/<controller>/5
        [HttpGet("posts/{id:int:min(1)}", RouteOrder = 3)]
        public BlogPost Get(int id)
        {
            return _blogRepository.GetById(id);
        }

        [HttpGet(@"{author:regex([\w+\s+])}", RouteOrder = 2)]
        public IEnumerable<BlogPost> Get(string author)
        {
            return _blogRepository.GetByAuthor(author);
        }

        [HttpGet("{date:datetime}", RouteOrder = 1)]
        public IEnumerable<BlogPost> Get(DateTime date)
        {
            return _blogRepository.GetByDate(date);
        }

        // POST api/<controller>
        public void Post([FromBody]BlogPost post)
        {
            _blogRepository.Add(post);
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]BlogPost post)
        {
            _blogRepository.Update(post);
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            _blogRepository.Delete(id);
        }
    }
}